package com.kfm.am;

import java.util.Comparator;
import java.util.Vector;

public class Test01 {

    private static Vector vector = new Vector(); // capacity 为10 的集合对象

    public static void main(String[] args) {
        // 创建一个Vector集合，用于存储学生的姓名。编写一个函数，接受一个学生姓名并将其添加到Vector中。
        // 然后编写另一个函数，接受一个学生姓名并从Vector中删除该姓名。最后，打印出Vector中所有的学生姓名。
        addStudent("张三", 20);
        addStudent("李四", 21);
        addStudent("王五", 22);
        addStudent("孙六", 19);
        System.out.println(vector.size());
        removeStudent("张三", 20);
        System.out.println(vector.size());
        sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student s1 = (Student) o1;
                Student s2 = (Student) o2;
                return s1.getAge() - s2.getAge();
            }
        });
        vector.forEach(obj -> System.out.println(obj));
    }

    public static boolean addStudent(String name, int age) {
        Student stu = new Student(name, age);
        return vector.add(stu);
    }

    public static boolean removeStudent(String name, int age) {
        Student stu = new Student(name, age);
        return vector.remove(stu);
    }

    public static void sort(Comparator c) {
        for (int i = 0; i < vector.size() - 1; i++) {
            for (int j = 0; j < vector.size() - 1 - i; j++) {
//                Student s1 = (Student)vector.get(j);
//                Student s2 = (Student)vector.get(j + 1);
                // s1.getAge() - s2.getAge() > 0
                // compare() 方法，你只需要传递给我两个对象，他们具体怎么比较，我来实现
                if (c.compare(vector.get(j), vector.get(j + 1)) > 0) { // s1的年龄> s2的年龄 s1.getAge() > s2.getAge()
                    // 交换位置
                    Object t = vector.get(j);
                    Object temp = vector.set(j, vector.get(j + 1)); // temp 是原来 j 位置的值
                    vector.set(j + 1, t);
                }
            }
        }
    }

    public static void insertSort(Comparator c) {
        int size = vector.size();
        for (int i = 0; i < size - 1; i++) {
//            Student s1 = (Student) vector.get(i + 1);
            int prevIndex = i;
            // while (prevIndex >= 0 && arr[prevIndex] > temp) {}
            // c.compare(vector.get(prevIndex), s1) > 0 说明 vector.get(prevIndex) > s1
            // vector.get(prevIndex).getAge() - vector.get(i + 1).getAge > 0
            while (prevIndex >= 0 && c.compare(vector.get(prevIndex), vector.get(i + 1)) > 0) {
                // arr[prevIndex + 1] = arr[prevIndex]
                vector.set(prevIndex + 1, vector.get(prevIndex));
                prevIndex--;
            }
            vector.set(prevIndex + 1, vector.get(i + 1));
        }
    }

//    public static void insertSort(Student[] arr, Comparator c) {
//        for (int i = 0; i < arr.length - 1; i ++) {
//            for (int j = 0; j < arr.length - 1 - i; j ++) {
////                if (arr[j].getAge() - arr[j + 1].getAge() > 0) {
////
////                }
//                if (c.compare(arr[j], arr[j +1]) > 0) {
//
//                }
//            }
//        }
//
//    }
}

